//
//  AllPageViewController.m
//  MoGuRadio
//
//  Created by dlios on 15/6/3.
//  Copyright (c) 2015年 mogu.com 蘑菇科技. All rights reserved.
//

// ViewControllers
#import "AllPageViewController.h"
#import "DetailPageViewController.h"
#import "AllDetailViewController.h"
#import "SpecialPageViewController.h"
#import "CollectPageViewController.h"
#import "MyRadioViewController.h"
#import "YRSideViewController.h"
#import "SearchPageViewController.h"
// 第三方
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "MJRefresh.h"
#import "MBProgressHUD.h"

// 自定义cell
#import "LBView.h"
#import "AllPageTableViewCell.h"
#import "FoucsTableViewCell.h"
#import "AllPageCollectionReusableView.h"
#import "AllPageCollectionViewCell.h"
#import "CollectTableViewCell.h"

//model
#import "BaseModel.h"
#import "AllPageData.h"
#import "MushRoom.h"
#import "DataBaseHandler.h"


// 重用池宏
#define COLLECT_REUSE @"MainCollectionViewReuseIdentifier"
#define TABLEVIEW_REUSE @"MainTableViewReuseIdentifier"
#define COLLECT_REUSEIDENTIFIER  @"collectReuseIdentifier"

@interface AllPageViewController () <UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource, collect, MBProgressHUDDelegate>
{
    AllPageData *page;
    NSInteger number;
    MBProgressHUD *_hud;
}

// 首页主滚动
@property (nonatomic, retain) UIScrollView *mainScrollView;


// 首页顶部轮播
@property (nonatomic, retain) UICollectionView *mainCollectionView;
@property (nonatomic, retain) UIScrollView *myScrollView;
@property (nonatomic, retain) UIImageView *myImageView;
@property (nonatomic, retain) UIView *mainView;
@property (nonatomic, retain) UIPageControl *pageControl;
@property (nonatomic, assign) NSInteger posterCurreentPage;
@property (nonatomic, retain) NSTimer *collecTimer;
@property (nonatomic, retain) NSMutableArray *posterArray;


// 定义一个可变数组, 一个不可变字典, 用来接收解析出的数据
@property (nonatomic, retain) NSMutableArray *moguArr;
@property (nonatomic, retain) NSDictionary *moguDic;
@property (nonatomic, retain) NSMutableArray *moguArray;
@property (nonatomic, retain) NSMutableArray *moguInSertArray;

// 二次网络请求
@property (nonatomic, retain) NSMutableArray *remmendArr;
@property (nonatomic, retain) NSDictionary *remmendDic;

// 图片地址数组
@property (nonatomic, retain) NSMutableArray *btnImageStr;
// 用来显示数据(表视图)
@property (nonatomic, retain) UITableView *myTableView;
// 分区名字
@property (nonatomic, retain) NSString *sectionName;

@property (nonatomic, assign) CGFloat lastContentOffset;

//@property (nonatomic, retain) AllPageData *page;

@end

@implementation AllPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
//    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:arc4random() % 255 / 255.0 green:arc4random() % 255 / 255.0  blue:arc4random() % 255 / 255.0 alpha:0.7];
    
    self.view.backgroundColor = [UIColor colorWithRed:0.135 green:0.376 blue:0.305 alpha:1.0];

    // 给UINavigationController添加图片
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"lvlvlvlv"] forBarMetrics:UIBarMetricsDefault];

    self.automaticallyAdjustsScrollViewInsets = NO;

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, LBVIEW_WIDTH1, LBVIEW_HEIGHT1)];
    imageView.image = [UIImage imageNamed:@"789.jpg"];
    [self.myTableView addSubview:imageView];
    [imageView release];

    [self setUpSubViews];
    [self dataHandler];
    [self dataHandlerHotFoucs];
    
// 下拉刷新
//    [self.myTableView addHeaderWithTarget:self action:@selector(dataHandler)];
//    [self.myTableView headerBeginRefreshing];
    
    // 导航栏左侧按钮
    UIImage *leftButtonImage = [UIImage imageNamed:@"liftFind@2x"];
    leftButtonImage = [leftButtonImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]initWithImage:leftButtonImage style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction:)] autorelease];
    
    // 导航栏右侧按钮
    UIImage *rightButtonImage = [UIImage imageNamed:@"search123@3x"];
    rightButtonImage = [rightButtonImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:rightButtonImage style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction:)];
    
    
    UIView *view = [[UIView alloc] initWithFrame:self.view.bounds];
    view.tag = 10001;
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    [self MBProgressHUD];
    
}

- (void)leftButtonAction:(UIBarButtonItem *)barButton
{
    YRSideViewController *ZHAIRUI = (YRSideViewController *)self.view.window.rootViewController;
    [ZHAIRUI showLeftViewController:YES];
}

- (void)rightButtonAction:(UIBarButtonItem *)barButton
{
    SearchPageViewController *searchVC = [[SearchPageViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.title = @"首页";
    
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.097 green:0.6701 blue:0.4134 alpha:1.0];
}

- (void)setUpSubViews
{
    
    // 用于存放轮播图片的UIView(myScrollView + pageControl)
    self.mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LBVIEW_WIDTH1, LBVIEW_HEIGHT1 / 3.3 + 30 * kYHeight)];
    self.mainView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
    
    
    
    // 用于首页轮播图片
    self.myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 30, VIEW_WIDTH, LBVIEW_HEIGHT1 / 3.3)];
    self.myScrollView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
    self.myScrollView.contentSize = CGSizeMake(VIEW_WIDTH * 7, 0);
    self.myScrollView.showsHorizontalScrollIndicator = NO;
    self.myScrollView.showsVerticalScrollIndicator = NO;
    self.myScrollView.pagingEnabled = YES;
    self.myScrollView.bounces = NO;
    self.myScrollView.tag = 1000;
    self.myScrollView.delegate = self;
//    self.myScrollView.bounces = YES;
    [self.mainView addSubview:self.myScrollView];
    [_myScrollView release];
    
    
     //用于控制myScrollView的滑动
//    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(VIEW_WIDTH / 2.5, VIEW_HEIGHT / 5, VIEW_WIDTH / 6, VIEW_HEIGHT / 10)];
//    self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
//    self.pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
//    self.pageControl.numberOfPages = 5;
//    self.pageControl.currentPage = 0;
//    [self.pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
//    [self.mainView addSubview:self.pageControl];
//    [_pageControl release];
    
    
    
    // 显示主页数据的表视图
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 34, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
    
    self.myTableView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
//    self.myTableView.scrollEnabled = NO;
    
    // 添加tableView的HeaderView
    self.myTableView.tableHeaderView = self.mainView;
    [self.view addSubview:self.myTableView];
    [_myTableView release];
    [_mainView release];
    
    
    
    [self.myTableView registerClass:[AllPageTableViewCell class] forCellReuseIdentifier:TABLEVIEW_REUSE];
    
    
    
     //自动播放轮播图片
    self.posterCurreentPage = 1;
    NSTimeInterval timeInterval = 3.0;
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(timeAction:) userInfo:nil repeats:YES];
    [timer fire];
    
}

#warning mark 页面控制scrollView

- (void)pageAction:(UIPageControl *)pageControl
{
    [self.myScrollView setContentOffset:CGPointMake((VIEW_WIDTH * (pageControl.currentPage + 1)), 0) animated:YES];
}

 //scrollView控制pageControl
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger pageNumber = (self.myScrollView.contentOffset.x - self.myScrollView.frame.size.width) / VIEW_WIDTH;
    
    _pageControl.currentPage = pageNumber;
}

- (void)timeAction:(NSTimer *)timer
{
    [self.myScrollView setContentOffset:CGPointMake(self.myScrollView.frame.size.width * self.posterCurreentPage, 0) animated:YES];
    self.posterCurreentPage++;
}


//  拖动循环
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == self.myScrollView) {
        //  判断循环
        if (scrollView.contentOffset.x >= self.myScrollView.frame.size.width * (self.posterArray.count - 1)) {
            self.myScrollView.contentOffset = CGPointMake(self.myScrollView.frame.size.width, 0);
        } else if(scrollView.contentOffset.x <= 0) {
            self.myScrollView.contentOffset = CGPointMake(self.myScrollView.frame.size.width * (self.posterArray.count - 2), 0);
        }
    }
}


- (void)handlePoster
{
    NSMutableArray *tempArr = [[self.moguArr objectAtIndex:0] objectForKey:@"dataList"];
    self.posterArray = [NSMutableArray arrayWithArray:tempArr];
    id firstPoster = [self.posterArray firstObject];
    id lastPoster = [self.posterArray lastObject];
    [self.posterArray insertObject:lastPoster atIndex:0];
    [self.posterArray addObject:firstPoster];
}

//  定制器循环
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    if (scrollView == self.myScrollView) {
        //  判断循环
        if (scrollView.contentOffset.x >= self.myScrollView.frame.size.width * (self.posterArray.count - 1)) {
            self.myScrollView.contentOffset = CGPointMake( self.myScrollView.frame.size.width, 0);
            self.posterCurreentPage = 2;
        }
    }
}


//上拉隐藏tabBar
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    self.lastContentOffset = scrollView.contentOffset.y;
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    if (self.lastContentOffset > scrollView.contentOffset.y) {
        
        self.tabBarController.tabBar.hidden = NO;
        
    } else {
        
        self.tabBarController.tabBar.hidden = YES;
    }
}


#warning mark UICollectionView 的协议方法

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyRadioViewController *myRadioVC = [MyRadioViewController shareInstance];
    
    if (self.remmendArr.count > indexPath.item) {
        
        myRadioVC.rname = [[[[[self.remmendArr objectAtIndex:indexPath.item] objectForKey:@"audioList"] firstObject] objectForKey:@"audioInfo"] objectForKey:@"audioName"];
        
        myRadioVC.playImage = [[[[[self.remmendArr objectAtIndex:indexPath.item] objectForKey:@"audioList"] firstObject] objectForKey:@"audioInfo"] objectForKey:@"albumPic"];
        
        myRadioVC.mp3Url = [[[[[self.remmendArr objectAtIndex:indexPath.item] objectForKey:@"audioList"] firstObject] objectForKey:@"audioInfo"] objectForKey:@"mp3PlayUrl"];
        
    }
    
    [self.navigationController pushViewController:myRadioVC animated:YES];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.remmendArr.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AllPageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:COLLECT_REUSE forIndexPath:indexPath];
    
    // 数组保护
    if (self.remmendArr.count > indexPath.item) {
        
        cell.myLabel.text = [[self.remmendArr objectAtIndex:indexPath.item] objectForKey:@"rname"];
        NSString *imageStr = [[self.remmendArr objectAtIndex:indexPath.item] objectForKey:@"pic"];
        NSURL *url = [NSURL URLWithString:imageStr];
        [cell.myImageView setImageWithURL:url];
    }
    
    return cell;
    
}


#warning mark UICollectionView的headerSection的协议方法
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        AllPageCollectionReusableView *reuseView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        
       reuseView.myLabel.text = @"热点推荐";
        
        return reuseView;
    }
    
    return nil;
    
}


#warning mark UITableView的协议方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        
        return 1;
        
    } else {

        return 3;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.f;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 7;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SpecialPageViewController *specialVC = [[SpecialPageViewController alloc] init];
    
    if (self.moguArr.count > indexPath.row) {
        
        specialVC.rid = [[[[[self.moguArr objectAtIndex:indexPath.section + 2] objectForKey:@"dataList"] objectAtIndex:indexPath.row] objectForKey:@"rid"] description];
        
        specialVC.pic = [[[[self.moguArr objectAtIndex:indexPath.section + 2] objectForKey:@"dataList"] objectAtIndex:indexPath.row] objectForKey:@"pic"];
        
        specialVC.hostName = [[[[[[self.moguArr objectAtIndex:indexPath.section + 2] objectForKey:@"dataList"] objectAtIndex:indexPath.row] objectForKey:@"host"] firstObject] objectForKey:@"name"];
        
        specialVC.foucsNum = [[[[[self.moguArr objectAtIndex:indexPath.section + 2] objectForKey:@"dataList"] objectAtIndex:indexPath.row] objectForKey:@"followedNum"] description];
        
        specialVC.listenNum = [[[[[self.moguArr objectAtIndex:indexPath.section + 2] objectForKey:@"dataList"] objectAtIndex:indexPath.row] objectForKey:@"listenNum"] description];
        
        specialVC.descRadio = [[[[[[[self.moguArr objectAtIndex:indexPath.section + 2] objectForKey:@"dataList"] objectAtIndex:indexPath.row] objectForKey:@"audioList"] firstObject] objectForKey:@"audioInfo"] objectForKey:@"audioDes"];
        
        specialVC.likedNum = [[[[[[[self.moguArr objectAtIndex:indexPath.section + 2] objectForKey:@"dataList"] objectAtIndex:indexPath.row] objectForKey:@"audioList"] firstObject] objectForKey:@"likedNum"] description];
        
        specialVC.upTime = [[[[[self.moguArr objectAtIndex:indexPath.section + 2] objectForKey:@"dataList"] objectAtIndex:indexPath.row] objectForKey:@"updateTime"] description];
       
        specialVC.titleLabel = [[[[self.moguArr objectAtIndex:indexPath.section + 2] objectForKey:@"dataList"] objectAtIndex:indexPath.row] objectForKey:@"rname"];
    
    }
    
    [self.navigationController pushViewController:specialVC animated:YES];

}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    if (section == 0) {
        
        LBView *sectionView = [[LBView alloc] init];
        page = [self.moguArray objectAtIndex:section + 2];
        sectionView.sectionLabel.text = @"--每日关注--";
        sectionView.sectionLabel.textColor = [UIColor whiteColor];
        return sectionView;
        
    } else if (section > 0){
        
        LBView *sectionView = [[LBView alloc] init];
        page = [self.moguArray objectAtIndex:section + 2];
        sectionView.sectionLabel.text = [NSString stringWithFormat:@"--%@--", page.labelName];
        sectionView.sectionLabel.textColor = [UIColor whiteColor];
        
        sectionView.rightImageView.image = [UIImage imageNamed:@"right@2x.png"];
        [sectionView.moreButton addTarget:self action:@selector(detailButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [sectionView.moreButton setTitle:@"更多" forState:UIControlStateNormal];
        sectionView.moreButton.tintColor = [UIColor whiteColor];
        
        // 给更多按钮设置tag值, 让它获取到当前的section分区
        sectionView.moreButton.tag = section + 2;
        
        return sectionView;
    }
    return nil;
}

- (void)detailButtonAction:(UIButton *)button
{
    AllDetailViewController *alldetailVC = [[AllDetailViewController alloc] init];
    
    page = [self.moguArray objectAtIndex:button.tag];
    //传值到详情页面
    alldetailVC.labelId = page.labelId;
    self.title = page.labelName;
    [self.navigationController pushViewController:alldetailVC animated:YES];
}


- (void)buttonAction:(UIButton *)button
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:button.tag % 10000 inSection:button.tag / 10000];
    
    DataBaseHandler *handler = [DataBaseHandler shareInstance];
    
    MushRoom *mushRoom = [[MushRoom alloc] init];
    
    mushRoom.rid = [[[[[self.moguArr objectAtIndex:button.tag / 10000] objectForKey:@"dataList"] objectAtIndex:button.tag % 10000] objectForKey:@"rid"] description];
    
    mushRoom.pic = [[[[self.moguArr objectAtIndex:button.tag / 10000] objectForKey:@"dataList"] objectAtIndex:button.tag % 10000] objectForKey:@"pic"];
    
    mushRoom.desLabelText = [[[[self.moguArr objectAtIndex:button.tag / 10000] objectForKey:@"dataList"] objectAtIndex:button.tag % 10000] objectForKey:@"radioDesc"];
    
    mushRoom.listenLabelText = [[[[[self.moguArr objectAtIndex:button.tag / 10000] objectForKey:@"dataList"] objectAtIndex:button.tag % 10000] objectForKey:@"listenNum"] description];
    
    mushRoom.focusLabelText = [[[[[self.moguArr objectAtIndex:button.tag / 10000] objectForKey:@"dataList"] objectAtIndex:button.tag % 10000] objectForKey:@"followedNum"] description];
    
    mushRoom.titleLabelText = [[[[self.moguArr objectAtIndex:button.tag / 10000] objectForKey:@"dataList"] objectAtIndex:button.tag % 10000] objectForKey:@"rname"];
    
    mushRoom.hostNameText = [[[[[[self.moguArr objectAtIndex:button.tag / 10000] objectForKey:@"dataList"] objectAtIndex:button.tag % 10000] objectForKey:@"host"] firstObject] objectForKey:@"name"];
    [handler insertMushRoom:mushRoom];
    NSLog(@"沙盒地址: %@",  NSHomeDirectory());
}


- (void)tapAction:(UITapGestureRecognizer *)tap
{
    
    MyRadioViewController *myRadioVC = [MyRadioViewController shareInstance];
    
    UIImageView *myImageView = (UIImageView *) tap.view;
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow: myImageView.tag % 10000 inSection:myImageView.tag / 10000];
    
    myRadioVC.playImage = [[[[[[[self.moguArr objectAtIndex:myImageView.tag / 10000] objectForKey:@"dataList"] objectAtIndex:myImageView.tag % 10000] objectForKey:@"audioList"] firstObject] objectForKey:@"audioInfo"] objectForKey:@"albumPic"];
    
    myRadioVC.rname = [[[[[[[self.moguArr objectAtIndex:myImageView.tag / 10000] objectForKey:@"dataList"] objectAtIndex:myImageView.tag % 10000] objectForKey:@"audioList"] firstObject] objectForKey:@"audioInfo"] objectForKey:@"albumName"];
    
    myRadioVC.mp3Url = [[[[[[[self.moguArr objectAtIndex:myImageView.tag / 10000] objectForKey:@"dataList"] objectAtIndex:myImageView.tag % 10000] objectForKey:@"audioList"] firstObject] objectForKey:@"audioInfo"] objectForKey:@"mp3PlayUrl"];
    
    [self.navigationController pushViewController:myRadioVC animated:YES];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        
        CollectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:COLLECT_REUSEIDENTIFIER];
        if (cell == nil) {
            
            cell = [[[CollectTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:COLLECT_REUSEIDENTIFIER] autorelease];
        }
        cell.delegate = self;
        return cell;
        
        
    } else {
        
    
    AllPageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TABLEVIEW_REUSE];
    
    if (self.moguArr.count > indexPath.row) {
        
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        
        NSString *imageStr = [[[[self.moguArr objectAtIndex:indexPath.section + 2] objectForKey:@"dataList"] objectAtIndex:indexPath.row] objectForKey:@"pic"];
        
        cell.playImageView.tag = (indexPath.section + 2) * 10000 + indexPath.row;
        [cell.playImageView addGestureRecognizer:tap];
        [tap release];
        [cell.mainImageView setImageWithURL:[NSURL URLWithString:imageStr]];
        
        
        UIImage *imageButton = [UIImage imageNamed:@"1.png"];
        // 渲染图片
        imageButton = [imageButton imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [cell.addButton setImage:imageButton forState:UIControlStateNormal];
        [cell.addButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside
         ];
        
        cell.addButton.tag = (indexPath.section + 2) * 10000 + indexPath.row;
        
        
        cell.likeLabel.text = @"关注";
        
        cell.titleLabel.text = [[[[self.moguArr objectAtIndex:indexPath.section + 2] objectForKey:@"dataList"] objectAtIndex:indexPath.row] objectForKey:@"rname"];
        
        cell.desLabel.text = [[[[self.moguArr objectAtIndex:indexPath.section + 2] objectForKey:@"dataList"] objectAtIndex:indexPath.row] objectForKey:@"radioDesc"];
        
        NSString *foucsStr = [[[[[self.moguArr objectAtIndex:indexPath.section + 2] objectForKey:@"dataList"] objectAtIndex:indexPath.row] objectForKey:@"followedNum"] stringValue];
        
        NSString *listenStr = [[[[[self.moguArr objectAtIndex:indexPath.section + 2] objectForKey:@"dataList"] objectAtIndex:indexPath.row] objectForKey:@"listenNum"] stringValue];
        
        cell.focusLabel.text = [BaseModel foucsChangeStringValue:foucsStr];
        cell.listenLabel.text = [BaseModel listenChangeStringValue:listenStr];
        
    }
        return cell;
    }
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath Arr:(NSArray *)sourceArr
{
        MyRadioViewController *myRadioVC = [MyRadioViewController shareInstance];
    
        if (self.remmendArr.count > indexPath.item) {
    
            myRadioVC.rname = [[[[[self.remmendArr objectAtIndex:indexPath.item] objectForKey:@"audioList"] firstObject] objectForKey:@"audioInfo"] objectForKey:@"audioName"];
    
            myRadioVC.playImage = [[[[[self.remmendArr objectAtIndex:indexPath.item] objectForKey:@"audioList"] firstObject] objectForKey:@"audioInfo"] objectForKey:@"albumPic"];
    
            myRadioVC.mp3Url = [[[[[self.remmendArr objectAtIndex:indexPath.item] objectForKey:@"audioList"] firstObject] objectForKey:@"audioInfo"] objectForKey:@"mp3PlayUrl"];
    
        }
    
        [self.navigationController pushViewController:myRadioVC animated:YES];
}


// cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        return LBVIEW_HEIGHT1 / 2;
    }
    
    return VIEW_HEIGHT / 6.5;
}


// 分区头的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 30.f;
    }
    return 30;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundColor = [UIColor colorWithRed:0.88 green:0.90 blue:0.738 alpha:0.4];
}


- (void)MBProgressHUD
{
    //加载MBProgressHUD
    UIView *view = [self.view viewWithTag:10001];
    _hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    _hud.delegate = self;
    _hud.color = [UIColor grayColor];
    _hud.labelText = @"加载中...";
    _hud.dimBackground = YES;
    _hud.removeFromSuperViewOnHide = YES;
    _hud.mode = MBProgressHUDModeIndeterminate;
}

#warning mark 第一次网络请求
- (void)dataHandler
{
    
    NSString *dataStr = @"http://api.kaolafm.com/api/v3.5/resource/list?ids=32_-25_5-28_2-30_3-31_3-23_3-22_3-26_3-27_3&operator=1&timestamp=1433409836&resolution=640*960&installid=0000SZ6c&version=3.5.1&appid=0&devicetype=1&page=200004&sign=F317B886396CA8621454351A56755FF7&sessionid=73D800875DFB456290B2DABD5FF2BAB4&udid=22D16F7F86EBC08060D1A4AE78E40A34&network=1&channel=appstore";
    dataStr = [dataStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", nil];
    
    [manger GET:dataStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        self.moguDic = [responseObject objectForKey:@"result"];
        self.moguArr = [self.moguDic objectForKey:@"dataList"];
        
        // 把dataList数组里的元素, 转成AllPageData类的对象
        NSMutableArray *dataArr = [[responseObject objectForKey:@"result"] objectForKey:@"dataList"];
        self.moguArray = [AllPageData modelArrWithDics:dataArr];
    
        [self imageHander];
//        [self.myTableView headerEndRefreshing];
        [self.myTableView reloadData];
        
        UIView *view = [self.view viewWithTag:10001];
        [view removeFromSuperview];
        [_hud hide:YES];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"错误信息是==%@", error);
        
    }];
    
}


- (void)imageHander
{
    [self handlePoster];
    
    for (NSInteger i = 0; i < self.posterArray.count; i++) {
        
        NSString *imageStr = [[self.posterArray objectAtIndex:i] objectForKey:@"pic"];
        
        NSURL *imageUrl = [NSURL URLWithString:imageStr];
        
        self.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(VIEW_WIDTH * i, 0, LBVIEW_WIDTH1, LBVIEW_HEIGHT1 / 3.3)];
        self.myImageView.backgroundColor = [UIColor whiteColor];
        
        self.myImageView.userInteractionEnabled = YES;
        
        [self.myImageView setImageWithURL:imageUrl];
        [self.myScrollView addSubview:self.myImageView];
        [_myImageView release];
    
     }
    
}


#warning mark 第二次网络请求 赋值给MainCollectionView
- (void)dataHandlerHotFoucs
{
    
    NSString *dataStr = @"http://api.kaolafm.com/api/v3.5/resource/list?ids=16_-17_-18_-14_5-19_-20_-15_5-21_&installid=0000TLD3&udid=eca85b7ac579ac2b4a4f9fec6744f8fd&sessionid=eca85b7ac579ac2b4a4f9fec6744f8fd1433842263948&imsi=460036090834220&operator=3&network=1&timestamp=1433842412&playid=0341feb3d321649db62216f79fcfa2f5&sign=f3e82c19c896a40926481f50f62d2a8c&appid=0&channel=A-baidu&version=3.6.1&devicetype=0&resolution=480*854&";
    
    dataStr = [dataStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", nil];
    
    [manger GET:dataStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    
            self.remmendDic = [responseObject objectForKey:@"result"];
            self.remmendArr = [[[self.remmendDic objectForKey:@"dataList"] objectAtIndex:2] objectForKey:@"dataList"];
        
//        [self.moguArr addObjectsFromArray:self.remmendArr];
        NSLog(@"%@", self.moguArr);
            
            [self.mainCollectionView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"错误信息是==%@", error);
        
    }];
    
}


- (void)dealloc
{
    _hud.delegate = nil;
    [_moguArray release];
    [_posterArray release];
    [_btnImageStr release];
    [_remmendArr release];
    [_remmendDic release];
    [_myImageView release];
    [_myScrollView release];
    [_mainView release];
    [_mainCollectionView release];
    [_mainScrollView release];
    [_pageControl release];
    [_collecTimer release];
    [_moguArr release];
    [_moguDic release];
    [_myTableView release];
    [_sectionName release];
    [super dealloc];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
